package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesCoreData;
import com.lebang.example.CencesCoreDataExample;
import com.lebang.model.MyConfig;
import com.lebang.service.cences.CencesCoreDataService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;


/**
 * 核心数据统计
 *
 * @author xwj
 * @date 2020-04-27
 */
@Controller
@RequestMapping("/sys/cences/cencescoredata")
public class CencesCoreDataController {
    @Autowired
    private CencesCoreDataService baseService;



    @Login
    @GetMapping("/list")
    public String list(Model model) {

        if(MyConfig.getEnv()==1){
            model.addAttribute("fishMark",1);
        }else{
            model.addAttribute("fishMark",0);
        }

        return "/cencescoredata/list";
    }



    /**
     * @param page     页数
     * @param limit    条数
     * @param star     开始日期
     * @param end      结束日期
     * @return 返回数据
     */
    @PostMapping("/page")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public HashMap newList(Long page, Integer limit, Integer star, Integer end,String appStore,Integer appId) {
        HashMap map = new HashMap(1);
        CencesCoreDataExample example = new CencesCoreDataExample();
        CencesCoreDataExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc");
        if (star != null && end != null) {
            c.andTimeBetween(star, end);
        }
        if(!TextUtil.isEmpty(appStore)){
            c.andAppStoreEqualTo(appStore);
        }else{
            c.andAppStoreEqualTo("all");
        }
        if(!TextUtil.isEmpty(appId)){
            c.andAppIdEqualTo(appId);
        }else{
            c.andAppIdEqualTo(MyConfig.getEnv());
        }

        map.put("count", this.baseService.countByExample(example));
        map.put("code", 0);

        List<CencesCoreData> masters = baseService.selectByExample(example);
        map.put("data", masters);
        return map;
    }


    @Login
    @PostMapping("/delete")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData delete(Integer id) {
        baseService.deleteByTime(baseService.selectById(id).getTime(),MyConfig.getEnv());
        return Callback.success("success", "删除成功");
    }


    /**
     * 统计指定日期渠道活跃人数
     *
     * @param date 日期
     * @return 返回结果
     */
    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end = LocalDate.parse(date, fmt);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
        return "success";
    }


}
